<template>
  <div>
    <!-- 头部 -->
    <div class="address-top">
      <router-link to="/addressList">取消</router-link>
      <p>添加好友</p>
      <p @click="send">发送</p>
    </div>
    <!-- 头部 end -->
    <div class="flex padd15 white top">
      <img :src="userInfo.Image">
      <div>
        <p class="name">{{userInfo.Nick?userInfo.Nick:userInfo.To_Account}}</p>
        <p class="sex">{{userInfo.Gender?userInfo.Gender:'保密'}}</p>
      </div>
    </div>
    <p class="message">附言</p>
    <textarea class="textarea" name="" id="" cols="30" rows="4" maxlength="40" placeholder="请输入你的附言(最多可输入40个汉子)"
              v-model="applyMes">{{applyMes}}</textarea>
  </div>
</template>
<style lang="less" rel="stylesheet/less" scoped>
  .font16 {
    font-size: .16rem;
  }

  .color4a {
    color: #4A4A4A;
  }

  .top {
    height: .8rem;
    align-items: center;
    img {
      border-radius: 50%;
      margin-right: .1rem;
      width: .5rem;
      height: .5rem;
    }
    .name {
      .font16;
      .color4a;
    }
    .sex {
      margin-top: .08rem;
      font-size: .14rem;
      color: #818181;
    }
  }

  .message {
    font-size: .14rem;
    color: #818181;
    line-height: .4rem;
    padding-left: .15rem;
  }

  .textarea {
    width: 100%;
    overflow: hidden;
    box-sizing: border-box;
    padding: .1rem .15rem;
  }

  textarea::-webkit-input-placeholder {
    .color4a;
    font-size: .14rem;
  }
</style>
<script>
  import {checkStatus} from '../../assets/js/login';
  import {Toast} from 'mint-ui'
  export default {
    data () {
      return {
        userInfo: {},  // 接收的信息
        applyMes: '', // 附言
        loginInfo: {}
      }
    },
    mounted () {
      this.userInfo = JSON.parse(this.$route.query.userInfo);
      console.log('this.userInfo',this.userInfo);
      checkStatus((loginInfo) => {
        this.loginInfo = loginInfo;
      });
    },
    methods: {
      // 返回
      back(){
        history.back()
      },
      // 发送
      send(){
        this.addFriendApi()
      },
      addFriendApi(){
        let applyAddFriend = (() => {
          let add_friend_item = [
            {
              To_Account: this.userInfo.To_Account,
              AddSource: "AddSource_Type_Unknow",
              AddWording: this.applyMes //加好友附言，可为空
            }
          ];
          let options = {
            From_Account: this.loginInfo.identifier,
            AddFriendItem: add_friend_item
          };

          webim.applyAddFriend(
            options,
            resp => {
              if (resp.Fail_Account && resp.Fail_Account.length > 0) {
                for (let i in resp.ResultItem) {
                  console.log(resp.ResultItem[i].ResultInfo);
                  break;
                }
              } else {
                Toast("好友请求已发送");
                setTimeout(()=>{
                  this.back();
                },1000)
              }
            },
            err => {
              console.log(err.ErrorInfo);
              if (err.ErrorInfo.indexOf('30525') > -1) {
                Toast("好友请求已发送");
                setTimeout(()=>{
                  this.back();
                },1000)
              }
              if (err.ErrorInfo.indexOf('30520') > -1) {
                Toast('该好友已添加');
              }
            }
          );
        })();
      }
    }
  }

</script>
